package com.controller;

import com.dao.BabyExchangeDao;
import com.dao.BabyGoodsDao;
import com.dao.BabyWellDao;
import com.entity.BabyExchangeDO;
import com.entity.BabyGoodsDO;
import com.entity.BabyWellDO;
import com.entity.dto.BabyExchangeDTO;
import com.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * ClassName：BabyWellController
 * Date：2022/10/22 19:40
 * Version：1.0
 * Description：
 * @author Administrator
 */
@RequestMapping("/v1/api/well")
@RestController
@CrossOrigin
public class BabyWellController {

    @Resource
    private BabyWellDao babyWellDao;

    @Resource
    private BabyExchangeDao babyExchangeDao;

    @Resource
    private BabyGoodsDao babyGoodsDao;

    @GetMapping("/find")
    public Result find() {
        try {
            List<BabyWellDO> list = babyWellDao.findAll();
            return Result.success(list.get(0));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询个人信息失败");
        }
    }

    @PutMapping("/edit")
    public Result edit(@RequestBody BabyWellDO babyWellDO) {
        try {
            babyWellDO.setId("1");
            babyWellDao.save(babyWellDO);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("编辑个人信息失败");
        }
    }

    @PutMapping("/edit/score")
    public Result editScore(Integer score) {
        try {
            List<BabyWellDO> list = babyWellDao.findAll();
            BabyWellDO babyWellDO = list.get(0);
            String thisTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            if (babyWellDO.getTime().equals(thisTime)) {
                return Result.error("今日已评价");
            }
            babyWellDO.setJf(babyWellDO.getJf()+score*20);
            babyWellDO.setTime(thisTime);
            babyWellDao.save(babyWellDO);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("评价失败");
        }
    }

    @GetMapping("/exchane/details")
    public Result exchengeDetailsList() {
        try {
            List<BabyExchangeDO> list = babyExchangeDao.findAll();
            List<BabyExchangeDTO> babyExchangeList = list.stream().map(x -> {
                BabyExchangeDTO babyExchangeDTO = new BabyExchangeDTO();
                Optional<BabyGoodsDO> optional = babyGoodsDao.findById(x.getLpbh());
                if (optional.isPresent()) {
                    babyExchangeDTO.setLpmc(optional.get().getLpm());
                    babyExchangeDTO.setId(x.getId());
                    babyExchangeDTO.setDhsj(x.getDhsj().toString().substring(0,19));
                }
                return babyExchangeDTO;
            }).collect(Collectors.toList());
            return Result.success(babyExchangeList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("兑换礼物详情查询失败");
        }
    }
}
